package com.me.mygdxgame.models;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.sun.org.apache.xpath.internal.operations.Mult;

public abstract class AbstractGameObject {

//	public Vector2 position;
//	public Vector2 dimension;
//	public Vector2 origin;
//	public Vector2 scale;
//	public float rotation;
	public Body body;
	public Vector2 velocity;
	public Vector2 terminalVelocity;
	public Vector2 friction;
	public Vector2 acceleration;
	public Rectangle bounds;
	public Sprite sprite;
	
	public AbstractGameObject(){
//		position = new Vector2();
//		dimension = new Vector2();
//		origin = new Vector2();
//		scale = new Vector2(1, 1);
//		rotation = 0;
		velocity = new Vector2();
		terminalVelocity = new  Vector2();
		friction = new Vector2();
		acceleration = new Vector2();
		bounds = new Rectangle();
		sprite = new Sprite();
//		sprite.setPosition(x, y)
	}
	
	protected void updateMotionX(float deltaTime){
		if(velocity.x != 0){
			if(velocity.x > 0){
				velocity.x = Math.max(velocity.x - friction.x*deltaTime, 0);
			}else{
				velocity.x = Math.min(velocity.x + friction.x*deltaTime, 0);
			}
			velocity.x += acceleration.x*deltaTime;
			velocity.x = MathUtils.clamp(velocity.x, -terminalVelocity.x, terminalVelocity.x);
		}
	}
	
	protected void updateMotionY(float deltaTime){
		if(velocity.y != 0){
			if(velocity.y > 0){
				velocity.y = Math.max(velocity.y - friction.y*deltaTime, 0);
			}else{
				velocity.y = Math.min(velocity.y + friction.y*deltaTime, 0);
			}
			velocity.y += acceleration.y*deltaTime;
			velocity.y = MathUtils.clamp(velocity.y, -terminalVelocity.y, terminalVelocity.y);
		}
	}
	
	public void update(float deltaTime){
//		updateMotionX(deltaTime);
//		updateMotionY(deltaTime);
		
		if(body == null){
		}else{
//			body.setLinearVelocity(5, 0);
//			body.getPosition().x += velocity.x*deltaTime;
//			body.getPosition().y += velocity.y*deltaTime;
//			System.out.println(body.getPosition().x);
//			position.set(body.getPosition().scl(BoxBuilder.BOX_TO_WORLD));
			Vector2 temp = body.getPosition().scl(BoxBuilder.BOX_TO_WORLD);
			sprite.setPosition(temp.x - sprite.getWidth()/2, temp.y - sprite.getHeight()/2);
//			rotation = body.getAngle()*MathUtils.radiansToDegrees;
			sprite.setRotation(body.getAngle()*MathUtils.radiansToDegrees);
		}
	}
	
	public abstract void render (SpriteBatch batch);
	
	public TextureRegion[] getTextures(int start, int end, TextureRegion[] temp){
		TextureRegion[] needTextures = new TextureRegion[end - start + 1];
		int index = 0;
		for(int i = start - 1; i < end; i++){
			needTextures[index] = temp[i];
			index++;
		}
		return needTextures;
	}
}
